package main.hsw.socialmedia.model.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import main.hsw.socialmedia.model.dao.UserDAO;
import main.hsw.socialmedia.model.dao.rowMapper.UserRowMapper;
import main.hsw.socialmedia.model.entity.User;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class UserDAOImpl extends DAOBase implements UserDAO{
	
	private final Log logger = LogFactory.getLog(UserDAOImpl.class);
	private final UserRowMapper userMapper = new UserRowMapper();

	@Override
	public boolean createUser(User user) {
		
		logger.debug("Erstellung vom User gestartet");
		
		boolean result = false;

		assert (user != null);

		String query = "INSERT INTO User (UserID, Vorname, Nachname, Mail, Passwort, "
				+ "Strasse, Hausnummer, Plz, Ort, Geschlecht, RegisterDate) VALUES " + "(?,?,?,?,?,?,?,?,?,?,?)";

		try {
			Connection con = openConnection();
			PreparedStatement prepStat = con.prepareStatement(query);

			int userID = getNextID(con, "User");
			logger.debug("new User has ID=" + userID);
			user.setUserID(userID);

			prepStat.setInt(1, user.getUserID());
			prepStat.setString(2, user.getVorname());
			prepStat.setString(3, user.getNachname());
			prepStat.setString(4, user.getMail());
			prepStat.setString(5, user.getPassword());
			prepStat.setString(6, user.getStrasse());
			prepStat.setString(7, user.getHausnummer());
			prepStat.setString(8, user.getPlz());
			prepStat.setString(9, user.getOrt());
			prepStat.setString(10, user.getGeschlecht());
			prepStat.setDate(11, new Date(new java.util.Date().getTime()));
			prepStat.execute();
			prepStat.close();
			con.close();

			result = true;
		} catch (SQLException e) {
			logger.error("SQLException occured", e);
			result = false;
		}
		return result;
	}

	@Override
	public User getUserByMail(String email) {
		
		logger.debug("entering getUser by eMail");
		User result = null;
		String query = "SELECT user.userid as userID, user.vorname as vorname, "
				+ "user.nachname as nachname, user.mail as email, user.passwort as passwort, "
				+ "user.strasse as strasse, user.hausnummer as hausnummer, user.plz as plz, user.ort as ort, "
				+ "user.geschlecht as geschlecht "
				+ "FROM user WHERE user.mail = ?";

		try {
			Connection con = openConnection();
			PreparedStatement prepStat = con.prepareStatement(query);
			prepStat.setString(1, email);
			ResultSet rs = prepStat.executeQuery();
			// Wir erwarten nur ein Ergebnis
			if (rs.next()) {
				result = this.userMapper.mapRow(rs);
			}
			rs.close();
			prepStat.close();
			con.close();
		} catch (SQLException e) {
			logger.error("SQLException occured", e);
			result = null;
		}
		return result;
	}

	@Override
	public User getUserByID(int id) {
		
		logger.debug("entering getUser by UserID");
		User result = null;
		String query = "SELECT user.userId as userID, user.vorname as vorname, "
				+ "user.nachname as nachname, user.mail as mail, user.passwort as passwort, "
				+ "user.strasse as strasse, user.hausnummer as hausnummer, user.plz as plz, user.ort as ort, "
				+ "user.geschlecht as geschlecht "
				+ "FROM user WHERE user.userid = ?";

		try {
			Connection con = openConnection();
			PreparedStatement prepStat = con.prepareStatement(query);
			prepStat.setInt(1, id);
			ResultSet rs = prepStat.executeQuery();
			// Wir erwarten nur ein Ergebnis
			if (rs.next()) {
				result = this.userMapper.mapRow(rs);
			}
			rs.close();
			prepStat.close();
			con.close();
		} catch (SQLException e) {
			logger.error("SQLException occured", e);
			result = null;
		}
		return result;
	}

	@Override
	public boolean updateUser(User user) {
logger.debug("Erstellung vom User gestartet");
		
		boolean result = false;

		assert (user != null);

		String query = "UPDATE User SET id=?, Vorname=?, Nachname=?, Email=?, Kennwort=?, "
				+ "Strasse=?, Hausnummer=?, Mail=?, Passwort=?, Geschlecht=?";

		try {
			Connection con = openConnection();
			PreparedStatement prepStat = con.prepareStatement(query);

			int personNr = getNextID(con, "User");
			logger.debug("new User has ID=" + personNr);
			user.setUserID(personNr);

			prepStat.setInt(1, user.getUserID());
			prepStat.setString(2, user.getVorname());
			prepStat.setString(3, user.getNachname());
			prepStat.setString(4, user.getMail());
			prepStat.setString(5, user.getPassword());
			prepStat.setString(6, user.getStrasse());
			prepStat.setString(7, user.getHausnummer());
			prepStat.setString(8, user.getPlz());
			prepStat.setString(9, user.getOrt());
			prepStat.execute();
			prepStat.close();
			con.close();

			result = true;
		} catch (SQLException e) {
			logger.error("SQLException occured", e);
			result = false;
		}
		return result;
	}

	@Override
	public boolean delete(User user) {
		logger.debug("entering delete user");
		boolean result = false;

		assert (user != null);

		String query = "DELETE FROM User WHERE userid = ?";

		try {
			Connection con = openConnection();
			PreparedStatement prepStat = con.prepareStatement(query);
			prepStat.setInt(1, user.getUserID());
			prepStat.execute();
			prepStat.close();
			con.close();

			result = true;

		} catch (SQLException e) {
			logger.error("SQLException occured", e);
			result = false;
		}
		return result;
	}

	@Override
	public boolean addFriend(User user, User friend) {
		// TODO Auto-generated method stub
		return false;
	}
}
